<?php
require_once(OC::$SERVERROOT . "/apps/reports/lib/helper.php");
require_once(OC::$SERVERROOT . "/apps/reports/lib/reports.php");

if (!PFactory::isMasterUser()) {
   echo '您无权访问该资源';
   exit();
}

$model = new PReportsModelReports();
$summary = $model->getSourceContrast();

if ($summary === false) {
   jexit('<div class="yahei large noreportsdata">' . ($model->getError() ? $model->getError() : '系统错误') . '</div>');
}

$xtypeList = array(
   'event' => array('title' => '面试率', 'mtext' => '收到的简历', 'ntext' => '进入面试'),
   'retest' => array('title' => '复试率', 'mtext' => '进入面试', 'ntext' => '进入复试'),
   'offer' => array('title' => '录用率', 'mtext' => '进入面试', 'ntext' => '发offer'),
   'entry' => array('title' => '入职率', 'mtext' => '发offer', 'ntext' => '入职')
);
$sourceList = array(
   'upload' => array('text' => '枇杷派', 'light' => 1),
   '51job' => array('text' => '前程无忧', 'light' => 1),
   'zhilian' => array('text' => '智联招聘', 'light' => 1),
   'lagou' => array('text' => '拉勾', 'light' => 0),
   'jiancai' => array('text' => '建筑英才', 'light' => 0)
);
$sources = JRequest::getVar('sources', array('upload', '51job', 'zhilian'), 'GET', 'array');
$showLabel = JRequest::getString('showLabel', 1);
?>
<? if ($showLabel): ?>
<div class="cotst_checkwrap">
   <? foreach ($sourceList as $source => $s): ?>
      <label class="checkbox inline <?=($s['light'] ? 'checkedbox' : '')?>">
         <input class="sourceSel" name="sources[]" value="<?=$source?>" type="checkbox" <?=(in_array($source, $sources) ? 'checked="checked"' : '')?>> <?=$s['text']?>
      </label>
   <? endforeach; ?>
</div>
<div class="noItemTips hide">请选择渠道来源</div>
<? endif; ?>

<div class="sourceContrastBars">
<? foreach ($xtypeList as $xtype => $x): ?>
   <div class="rep_resumefrom" <?= $xtype=='event' ? 'style="margin-top:20px;"' : '' ?>  id="anchor_ctrstsp_<?=$xtype?>">
      <h5><b><?=$x['title']?></b></h5>

      <div class="clearfix">
         <div id="<?=$xtype?>SourceBar" data-xtype="<?=$xtype?>" class="sourceBar pull-left" style="height:205px; width:60%"></div>

         <div class="rep_cotsttable pull-right" style="width:35%; margin-top:10px;">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
               <tr>
                  <th width="%" align="left"><i></i> 排行</th>
                  <th width="%"><?=$x['mtext']?></th>
                  <th width="%"><?=$x['ntext']?></th>
                  <th width="%"><?=$x['title']?></th>
               </tr>
               <?
               foreach ($summary['bars'][$xtype]['data'] as $i => $weight): 
                  $s = $summary['bars'][$xtype]['sources'][$i];
               ?>
                  <tr>
                     <td class="tleft medium"><b><?=($i+1)?></b> <?=$sourceList[$s]['text']?></td>
                     <td><?=$summary['total'][$xtype][$s]['m']?></td>
                     <td><?=$summary['total'][$xtype][$s]['n']?></td>
                     <td><?=$weight?>%</td>
                  </tr>
               <? endforeach; ?>
            </table>
         </div>
      </div>
   </div>
<? endforeach; ?>
</div>

<script type="text/javascript">
$(document).ready(function(){
   var summary = <?=json_encode($summary)?>;
   var charts = new Array();
   $(window).unbind('resize');
   
   $('.sourceBar').each(function(){
      var xtype = $(this).data('xtype');
      charts[charts.length++] = $.jqplot($(this).attr('id'), [summary.bars[xtype].data], {
         // The "seriesDefaults" option is an options object that will
         // be applied to all series in the chart.
         seriesDefaults:{
            renderer:$.jqplot.BarRenderer,
            rendererOptions: {
               fillToZero: true,
               barWidth: 45,
               barMargin: 70
            },
            pointLabels: {show: true},
            showLabel:false,
            shadow:false
         },
         axesDefaults: {
            tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
            tickOptions: {
               fontSize: '13px'
            }
         },
         // Custom labels for the series are specified with the "label"
         // option on the series option.  Here a series option object
         // is specified for each series.
         series:[
            {showLabel:false,color:'#71c5f4'}
         ],
         // Show the legend and put it outside the grid, but inside the
         // plot container, shrinking the grid to accomodate the legend.
         // A value of "outside" would not shrink the grid and allow
         // the legend to overflow the container.
         legend: {
            show: true,
            placement: 'outsideGrid'
         },
         axes: {
            // Use a category axis on the x axis and use our custom ticks.
            xaxis: {
                renderer: $.jqplot.CategoryAxisRenderer,
                ticks: summary.bars[xtype].ticks
            },
            // Pad the y axis just a little so bars can get close to, but
            // not touch, the grid boundaries.  1.2 is the default padding.
            yaxis: {
               min:0,
               max: 120,
               pad: 1.00,
               tickInterval: 20,
               tickOptions: {formatString: '%d%'}
            }
         },
         grid: {
            shadow: false,               // draw a shadow for grid.
            drawGridLines: false,        // wether to draw lines across the grid or not.
            gridLineColor: '#e9e9e9',   // *Color of the grid lines.
            background: '#ffffff',      // CSS color spec for background color of grid.
            borderColor: '#333333',     // CSS color spec for border around grid.
            borderWidth: 0.5,              // draw a shadow for grid.
         },
      });
   });

   $(window).resize(function(){
      $.each(charts, function(i, c){
         c.replot( { resetAxes: { xaxis: true } } );
      });
   });
});
</script>